package com.example.bingobox.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.LocationManager;
import android.os.Bundle;

import androidx.core.app.ActivityCompat;

import com.example.bingobox.model.State;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class LocationUtils {
    private static final long MIN_TIME = 1000;  // 1秒更新一次
    private static final float MIN_DISTANCE = 10;  // 10米更新一次
    
    public interface LocationCallback {
        void onLocationReceived(State.Location location);
        void onLocationFailed(String error);
    }

    public static void getCurrentLocation(Context context, LocationCallback callback) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        
        if (ActivityCompat.checkSelfPermission(context, 
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            callback.onLocationFailed("未获取定位权限");
            return;
        }

        android.location.LocationListener locationListener = new android.location.LocationListener() {
            @Override
            public void onLocationChanged(android.location.Location location) {
                String cityName = getCityName(context, location.getLatitude(), location.getLongitude());
                callback.onLocationReceived(new State.Location(
                    cityName,
                    location.getLatitude(),
                    location.getLongitude()
                ));
                locationManager.removeUpdates(this);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {}

            @Override
            public void onProviderEnabled(String provider) {}

            @Override
            public void onProviderDisabled(String provider) {
                callback.onLocationFailed("定位服务已禁用");
            }
        };

        try {
            locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                MIN_TIME,
                MIN_DISTANCE,
                locationListener
            );
        } catch (SecurityException e) {
            callback.onLocationFailed("获取位置信息失败: " + e.getMessage());
        }
    }

    private static String getCityName(Context context, double latitude, double longitude) {
        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses != null && !addresses.isEmpty()) {
                Address address = addresses.get(0);
                return address.getLocality(); // 获取城市名
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "未知城市";
    }
} 